import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-10-11
 * Time: 22:04
 */
class User {
    public String username;
    public String password;
}
@WebServlet("/json")
public class JsonServlet extends HttpServlet {
    //客户端body使用json合适来进行传输
//    {
//        "username": "zhangsan",
//        "password": "123"
//    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.readValue(req.getInputStream(), User.class);
        System.out.println("username: " + user.username + " password: " + user.password);
        resp.getWriter().write("ok");

        String s = objectMapper.writeValueAsString(user);
        System.out.println(s);
        resp.getWriter().write("ok");
    }
}
